package com.zy.system.service.impl;

import java.util.*;

import com.zy.common.utils.StringUtils;
import com.zy.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zy.system.mapper.SysMenuMapper;
import com.zy.system.domain.SysMenu;
import com.zy.system.service.ISysMenuService;
import com.zy.common.support.Convert;

/**
 * 菜单权限 服务层实现
 *
 * @author zy
 * @date 2018-12-19
 */
@Service
public class SysMenuServiceImpl implements ISysMenuService
{
    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    @Autowired
    private SysMenuMapper menuMapper;

    /**
     * 查询菜单权限信息
     *
     * @param menuId 菜单权限ID
     * @return 菜单权限信息
     */
    @Override
    public SysMenu selectSysMenuById(Integer menuId)
    {
        return menuMapper.selectSysMenuById(menuId);
    }

    /**
     * 查询菜单权限列表
     *
     * @param sysMenu 菜单权限信息
     * @return 菜单权限集合
     */
    @Override
    public List<SysMenu> selectSysMenuList(SysMenu sysMenu)
    {
        return menuMapper.selectSysMenuList(sysMenu);
    }

    /**
     * 新增菜单权限
     *
     * @param sysMenu 菜单权限信息
     * @return 结果
     */
    @Override
    public int insertSysMenu(SysMenu sysMenu)
    {
        return menuMapper.insertSysMenu(sysMenu);
    }

    /**
     * 修改菜单权限
     *
     * @param sysMenu 菜单权限信息
     * @return 结果
     */
    @Override
    public int updateSysMenu(SysMenu sysMenu)
    {
        return menuMapper.updateSysMenu(sysMenu);
    }

    /**
     * 删除菜单权限对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysMenuByIds(String ids)
    {
        return menuMapper.deleteSysMenuByIds(Convert.toStrArray(ids));
    }


    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectPermsByUserId(Long userId)
    {
        List<String> perms = menuMapper.selectPermsByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms)
        {
            if (StringUtils.isNotEmpty(perm))
            {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<SysMenu> selectMenusByUser(SysUser user)
    {
        List<SysMenu> menus = new LinkedList<>();
        // 管理员显示所有菜单信息
        if (user.isAdmin())
        {
            menus = menuMapper.selectMenuNormalAll();
        }
        else
        {
            menus = menuMapper.selectMenusByUserId(user.getUserId());
        }
        return getChildPerms(menus, 0);
    }

    public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId)
    {
        List<SysMenu> returnList = new ArrayList<>();
        for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext(); )
        {
            SysMenu t = iterator.next();
            if (t.getParentId() == parentId)
            {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<SysMenu> list, SysMenu t)
    {
        // 得到子节点列表
        List<SysMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysMenu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<SysMenu> it = childList.iterator();
                while (it.hasNext())
                {
                    SysMenu n = (SysMenu) it.next();
                    recursionFn(list, n);
                }
            }
        }

    }

    /**
     * 得到子节点列表
     */
    private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t)
    {
        List<SysMenu> tList = new ArrayList<>();
        Iterator<SysMenu> it = list.iterator();
        while (it.hasNext())
        {
            SysMenu n = (SysMenu) it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue())
            {
                tList.add(n);
            }
        }
        return tList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysMenu> list, SysMenu t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
